package com.aaa.train.controller;


import com.aaa.common.entity.LayUiTable;
import com.aaa.common.entity.ResultData;
import com.aaa.common.entity.TbCourse;
import com.aaa.common.entity.TbTrainer;
import com.aaa.train.service.TbCourseService;
import com.aaa.train.service.TbTrainerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 培训班老师 前端控制器
 * </p>
 *
 * @author wang
 * @since 2020-07-29
 */
@Controller
@RequestMapping("/tbTrainer")
public class TbTrainerController {

    @Autowired
    private TbTrainerService tbTrainerService;

    @Autowired
    private TbCourseService tbCourseService;

    @ResponseBody
    @RequestMapping("/selectAll")
    public LayUiTable selectTraineAll(Integer page, Integer limit,String tbTrainerName){
       LayUiTable layUiTable= tbTrainerService.selectTrainerAll(page,limit ,tbTrainerName);
       return layUiTable;
    }
    @ResponseBody
    @RequestMapping("/insertTrain")
    public ResultData insertTrain(TbTrainer tbTrainer){
         return tbTrainerService.insertTrain(tbTrainer);
    }
    @ResponseBody
    @RequestMapping("/selectTrainById")
    public LayUiTable selectTrainById(TbTrainer tbTrainer){
        return tbTrainerService.selectTrainById(tbTrainer);
    }
    @ResponseBody
    @RequestMapping("/updateTrain")
    public ResultData updateTrain(TbTrainer tbTrainer){
        return tbTrainerService.updateTrain(tbTrainer);
    }
    @ResponseBody
    @RequestMapping("/selectName")
    public ResultData selectName(TbTrainer tbTrainer){

        return tbTrainerService.selectName(tbTrainer);
    }
    @ResponseBody
    @RequestMapping("/delete")
    public ResultData delete(TbTrainer tbTrainer){
        return tbTrainerService.delete(tbTrainer);
    }
    @ResponseBody
    @RequestMapping("/deleteAll")
    public ResultData deleteAll(@RequestBody List<Map> tbTrainers){
        List<TbTrainer> listNew = new ArrayList<>();
        for (Map map : tbTrainers) {
            TbTrainer tbTrainer = new TbTrainer();
            tbTrainer.setTbTrainerId(Integer.parseInt(map.get("tb_trainer_id").toString()));
            tbTrainer.setStatus(1);
            listNew.add(tbTrainer);
        }
        boolean id = tbTrainerService.updateBatchById(listNew);
        if (id){
            return  new ResultData(true,"删除成功",null);
        }else{
            return  new ResultData(false,"删除失败",null);

        }
    }

    @ResponseBody
    @RequestMapping("/selectCourse")
    public ResultData selectCourse( ){
        List<TbCourse> tbCourses = tbCourseService.selectList(null);
        return new ResultData(true,"成功",tbCourses);
    }
}

